import {
	RouterMount,
	createRouter
} from 'uni-simple-router';
import store from '@/store/index.js'
import utils from '@/utils/index.js'


const router = createRouter({
	platform: process.env.VUE_APP_PLATFORM,
	routerErrorEach: (error, router) => {
		if (error.type === 0) {
			// next(false) 时不提示报错
			router.$lockStatus = false;
		}
	},
	routes: [...ROUTES]
});
//全局路由前置守卫
router.beforeEach(async (to, from, next) => {
	// 获取登录状态
	let userInfo = store.state.userInfo;

	// 访问页需要登录，但用户未登录
	if (to.meta.auth && !userInfo) {
		// 如果未登录，拒绝访问
		if (!await utils.reqUserInfoAuth()) return next(false);
	}
	// 放行
	next();
});
// 全局路由后置守卫
router.afterEach((to, from) => {
	console.log('跳转结束')
})

export {
	router,
	RouterMount
}